package com.studerb.tests.service;

import javax.swing.JOptionPane;

import org.junit.Test;
import static org.junit.Assert.*;

import com.studerb.exceptions.ServiceException;
import com.studerb.model.Server;
import com.studerb.service.ServerService;

public class TestServerService {

	protected ServerService serverService;

	protected String[] getConfigLocations() {
		return new String[] { "applicationContext.xml" };
	}

	public void setServerServer(ServerService serverService) {
		this.serverService = serverService;
	}

	@Test
	public void testCrudOps() {
		Server server = new Server("some.host", new Integer(119), new Integer(5), "user", "pass",
				false, true);
		try {
			serverService.deleteAllServers();
			if (serverService == null)
				throw new RuntimeException("Server Service is NULL");
			int oldCount = serverService.getServers().size();
			serverService.addServer(server);
			assertNotNull(server.getId());
			int newCount = serverService.getServers().size();
			assertEquals(oldCount + 1, newCount);
			serverService.deleteServer(server);
			newCount = serverService.getServers().size();
			assertEquals(oldCount, newCount);
		} catch (Exception e) {
			e.printStackTrace();
			fail("Should not have thrown exception testing server crud operations");
		}
	}

	@Test
	public void testGetMasterServer() {
		Server server = new Server("some.host", new Integer(119), new Integer(5), "user", "pass",
				false, true);
		try {
			serverService.deleteAllServers();
			serverService.addServer(server);
			assertTrue(server.isMaster());
			Server newServer = serverService.getMasterServer();
			assertTrue(newServer.isMaster());
			assertEquals(server.getId(), newServer.getId());
			serverService.deleteServer(newServer);
			server.setMaster(false);
			// test to make sure a server specifically set to not be master
			// is actual set to master if there are no other servers defined
			assertEquals(serverService.getServers().size(), 0);
			serverService.addServer(server);
			newServer = serverService.getMasterServer();
			assertTrue(newServer.isMaster());
			serverService.deleteServer(server);
		} catch (ServiceException e) {
			e.printStackTrace();
			fail("Should not have thrown exception testing getMasterServer");
		}

	}

	@Test
	public void testUpdateServer() {
		Server server = new Server("some.host", new Integer(119), new Integer(5), "user", "pass",
				false, true);
		Server newServer = new Server("another.host", new Integer(119), new Integer(3), "user",
				"pass", true, true);
		// this should update the server as the master because it is the first
		try {
			serverService.addServer(server);
			assertNotNull(server.getId());
			assertTrue(server.isMaster());
		} catch (Exception e) {
			fail("Should not have caught exception when updating good server");
		}
		// Now try to update the master to non-master
		try {
			server.setMaster(false);
			serverService.updateServer(server);
			fail("Should have throw exception on setting master to non-master");
		} catch (Exception e) {
		}

		try {
			serverService.addServer(newServer);
			assertFalse(server.isMaster());
			assertTrue(newServer.isMaster());
		} catch (Exception e) {
			fail("should not have caught exception on silently changing the master server");
		}
	}

	@Test
	public void TestTestServer() {
		Server server = new Server("news.giganews.com", new Integer(119), new Integer(9), "gn115371", "n!iC3n", false, true);
		try {
			JOptionPane.showMessageDialog(null, "Make sure internet is connected...");
			serverService.testServer(server);
		} catch (Exception e) {
			e.printStackTrace();
			fail("Caught exception " + e.getMessage());
		}
	}

	@Test
	public void testGetMaxPossibleConnections() {
		try {
			serverService.deleteAllServers();
			int count = serverService.getMaxConnections();
			assertTrue(count == 0);

			Server server1 = new Server("some host", 19, 50, null, null, true, false);
			Server server2 = new Server("some other host", 19, 5, null, null, false, false);
			serverService.addServer(server1);
			serverService.addServer(server2);

			count = serverService.getMaxConnections();
			assertTrue(count == 55);
		} catch (Exception e) {
			e.printStackTrace();
			fail("Should not have caught exception: " + e.getMessage());
		}
	}

}
